import { login, logout, getInfo , getInfo2} from '@/api/userLogin'
import {
    getToken,
    setToken,
    removeToken,
    removeCustomerId,
    setCustomerId,
    getCustomerId,
    setCustomerName, setCustomerAvatar, removeCustomerName, removeCustomerAvatar
} from '@/utils/auth'
import defAva from '@/assets/images/profile.jpg'
import {defineStore} from "pinia";

const useCustomerStore = defineStore(
    'customer',
    {
        state: () => ({
            token: getToken(),
            id: '',
            name: '',
            avatar: '',
            roles: [],
            permissions: [],
            //顾客信息
            customer:{}


        }),
        getters: {
            getAvatar(state) {
                if(getToken()){
                    // console.log("计算属性获取头像")
                    if(getCustomerId()){
                        console.log("用户已登录")
                        if(state.avatar==""){
                            console.log("页面刷新丢失图片信息")
                            state.getInfo(getToken())
                        }
                        console.log(state.avatar)
                        return state.avatar
                    }else{
                        // 没有登录
                        state.avatar ='https://cdn.wantuju.com/pngsucai/imgRegular/20210801/787ccfb6-42fb-48bd-ad8f-6ad4da7ea92b%20_49690_wantuju.jpg'
                    }

                }else{
                    //没有token
                    removeCustomerId()
                    state.avatar ='https://cdn.wantuju.com/pngsucai/imgRegular/20210801/787ccfb6-42fb-48bd-ad8f-6ad4da7ea92b%20_49690_wantuju.jpg'
                }
                console.log(state.avatar)
                return state.avatar
            },
        },
        actions: {


            // 登录
            login(userInfo) {
                const customerName = userInfo.customerName.trim();
                const customerPwd = userInfo.customerPwd;
                const code = userInfo.code;
                const uuid = userInfo.uuid;

                return new Promise((resolve, reject) => {
                    login(customerName, customerPwd, code, uuid).then(res => {
                        console.log("是否获取到token 和id=============>login.res.token"+res.token+"   "+res.CustomerId)
                        console.log(res)
                        //登陆后保存信息到cookie中
                        setToken(res.token)
                        setCustomerId(res.CustomerId)
                        setCustomerName(res.customerName)
                        setCustomerAvatar(res.customerAvatar)

                        this.token = res.token
                        this.getInfo(this.token)
                        //继续执行
                        resolve()
                    }).catch(error => {
                        console.log("失败")
                        reject(error)
                    })
                })
            },
            // 获取用户信息
            getInfo(token) {
                return new Promise((resolve, reject) => {
                    getInfo(token).then(res => {
                        console.log(res)
                        const avatar = (res.avatar == "" || res.avatar == null) ? defAva : import.meta.env.VITE_APP_BASE_API + res.avatar;
                        this.id = res.id
                        this.name = res.username
                        this.avatar = avatar
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 退出系统
            logOut() {
                return new Promise((resolve, reject) => {
                    console.log("customer logOut")
                    logout(this.token).then(() => {
                        console.log("logout scuessed")
                        this.token = ''
                        this.id = ''
                        this.avatar = ''
                        removeToken()
                        removeCustomerId()
                        removeCustomerName()
                        removeCustomerAvatar()

                        resolve()
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 使用该插件，开启数据缓存
            // persist: {
            //     enabled: true,
            //     strategies: [
            //         // { storage: sessionStorage, paths: ['firstName', 'lastName'] }, // firstName 和 lastName字段用sessionStorage存储
            //         { storage: localStorage, paths: ['id'] }, // accessToken字段用 localstorage存储
            //     ],
            // }

            //个人主页
            //获取个人信息
            getInfo2(token) {
                return new Promise((resolve, reject) => {
                    getInfo2(token).then(res => {
                        this.customer = res.data
                        console.log("个人中心页面")
                        console.log(res.data)

                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
        }
    })


export default useCustomerStore
